/*
 *  $Id: CurseOfDespair.cs 1221 2010-11-30 16:54:52Z thenn.erannor $
 *	This file is a part of Ragima CCG.
 *	(C) 2008 Ragima development group.
 *
 *	Licensed under the terms of the GNU GPL License version 2.
 *
 */

namespace Ragima.Cards001.Darkness.Spells {
    public class CurseOfDespair: EnchantmentCard<ChainData> {
        public CurseOfDespair(): base("001_d_s_curse_of_despair") {
            Level = CompleteLevel.ReadyForTest;
            Element = ElementType.Darkness;
            Value = 100;
            Target = new ChainTarget(null,
                new JogTarget(GetTargetPhrase(), GetJogValue),
                GraveyardTarget.Sacrifice,
                CreatureTarget.Both);
            AddManaCost(ElementType.Darkness, 1);
            AddCondition(new GraveyardCondition(false, false, 1));
        }

        private static IJogValueProvider GetJogValue(Player player) {
            return new ManaJogValueProvider(player, ElementType.Darkness, 1, 1, -1);
        }

        private int _value;

        protected override void DoAction(TargetArgs<ChainData> args) {
            TargetArgs<int> ja;
            TargetArgs<GraveyardData> ga;
            TargetArgs<Creature> ca;
            if (!args.Target.GetArgs(out ja, out ga, out ca)) return;

            ga.Target.MoveToSecondDeck();
            _value = ja.Target;
            args.Player.DecrementMana(ElementType.Darkness, _value);
            base.DoAction(args);
        }
        protected override AbstractObject GetTarget(TargetArgs<ChainData> args) {
            TargetArgs<Creature> ca;
            if (!args.Target.GetArgs(2, out ca)) return null;
            return ca.Target;
        }
        protected override EnchantmentAffect CreateEnchantment() {
            return new CurseOfDespairAffect(_value);
        }

        private class CurseOfDespairAffect: EnchantmentAffect<CurseOfDespair> {
            public int Value;

            public CurseOfDespairAffect(int value): base(true) {
                DefaultDescription = true;
                Value = value;
                Add(new Affects.Fear(Value));
                Add(new Affects.OnHit(DoHit, AffectSummaryType.Local));
            }

            private void DoHit(Creature hitter, AbstractObject target, int damage) {
                hitter.Owner.IncrementMana(ElementType.Darkness, Value);
            }

            public override string DescriptionText {
                get { return Description.GetText(Value); }
            }

            public override Affect Clone() {
                return new CurseOfDespairAffect(Value);
            }
        }
    }
}
